package com.cjg.plugin_app

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.cjg.common_lib.IClientCallback
import com.cjg.common_lib.IRemoteService


class PluginMainActivity : AppCompatActivity() {
    private lateinit var tv: TextView
    private var mService: IRemoteService? = null

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_plugin_main)
        tv = findViewById(R.id.tv)
    }

    // 客户端回调实现
    private val mCallback: IClientCallback = object : IClientCallback.Stub() {
        @Throws(RemoteException::class)
        override fun onMessageFromService(message: String) {
            // 注意：此方法在 Binder 线程池中执行，需要切换到主线程更新 UI
            runOnUiThread {
                tv.text = message
            }
        }
    }

    // 服务连接实现
    private val mConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            mService = IRemoteService.Stub.asInterface(service)
            try {
                // 注册回调
                mService?.registerCallback(mCallback)
                Log.e("client---->", "Service connected")
            } catch (e: RemoteException) {
                Log.e("client---->", "Error registering callback", e)
            }
        }

        override fun onServiceDisconnected(name: ComponentName) {
            mService = null
            Log.e("client---->", "Service disconnected")
        }
    }

    @SuppressLint("WrongConstant")
    override fun onStart() {
        super.onStart()
        // 绑定服务
        val intent = Intent()
        intent.action = "com.cjg.shadow.IRemoteService"
        intent.setPackage(packageName)
        bindService(intent, mConnection, BIND_AUTO_CREATE)
    }

    override fun onStop() {
        super.onStop()
        // 解绑服务并注销回调
        if (mService != null) {
            try {
                mService!!.unregisterCallback(mCallback)
            } catch (e: RemoteException) {
                Log.e("client---->", "Error unregistering callback")
            }
            unbindService(mConnection)
            mService = null
        }
    }

    fun toServer(view: View) {
        sendMessageToService();
    }

    var index = 0

    private fun sendMessageToService() {
        val message: String = index.toString()
        if (mService != null) {
            try {
                // 向服务发送消息
                mService!!.sendMessageToService(message)
            } catch (e: RemoteException) {
                Log.e("client---->", "Error sending message to service")
            }
        } else {
            Log.e("client---->", "Service not connected")
        }
        index++
    }

}